package com.example.lotterysystem.service.impl;

import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageListParam;
import com.example.lotterysystem.dao.dataobject.PrizeDO;
import com.example.lotterysystem.dao.mapper.PrizeMapper;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile prizePic) {
        //上传数据库
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setDescription(param.getDescription());
        prizeDO.setName(param.getPrizeName());
        prizeDO.setPrice(param.getPrice());

        //保存图片
        String fileName =  pictureService.savePicture(prizePic);
        prizeDO.setImageUrl(fileName);

        prizeMapper.insertPrize(prizeDO);

        return prizeDO.getId();

    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageListParam param) {
        // 计算总量
        int count = prizeMapper.count();

        //查询当前列表
        List<PrizeDTO> prizeDTOList = new ArrayList<>();
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(param.offset(),param.getPageSize());
        for (PrizeDO prizeDO: prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setId(prizeDO.getId());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTOList.add(prizeDTO);
        }
        return new PageListDTO(count,prizeDTOList);
    }
}
